package com.rtx.edi.platform.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rtx.edi.model.apibaseinfo.po.PlApibaseinfo;


public class SqlMappingConvert {

	private String targetJSON;
	private PlApibaseinfo targetApi;
	private List<String> targetSql;
	private Map<String, Map<String, Object>> globalMap;
	
	public SqlMappingConvert(String targetJSON, PlApibaseinfo plApibaseinfo,Map<String, Map<String, Object>> globalMap,List<String> targetSql) {
		super();
		this.targetJSON = targetJSON;
		this.targetApi = plApibaseinfo;
		this.globalMap = globalMap;
		this.targetSql = targetSql;
	}
	
	
	public void convertSql() {
		JSONObject target = JSON.parseObject(targetJSON);
		JSONObject tableConfig = JSON.parseObject(targetApi.getTableConfig());
		String mainTable = tableConfig.getString("maintable");

		List<String> coulmns = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		for(String column:target.keySet()) {
			String targetValues = target.getString(column);
			if("明细字段".equals(column)||!StringUtils.hasLength(targetValues)) {
				continue;
			}
			coulmns.add(column);
			values.add("'"+targetValues+"'");
		}
		if(coulmns.size() > 0) {
			String mSql = "insert into "+mainTable+"("+coulmns.stream().collect(Collectors.joining(","))+")values("+values.stream().collect(Collectors.joining(","))+")";
			targetSql.add(mSql);
		}
		
		if(target.containsKey("明细字段")) {
			List<String> dCoulmns = new ArrayList<String>();
			List<String> dValues = new ArrayList<String>();
			
			String subTable = tableConfig.getString("subtable");
			List<JSONObject> details =JSON.parseArray(target.getString("明细字段"), JSONObject.class);
			for(JSONObject detail:details) {
				for(String column:detail.keySet()) {
					String targetValues = target.getString(column);
					if(!StringUtils.hasLength(targetValues)) {
						continue;
					}
					dCoulmns.add(column);
					dValues.add("'"+targetValues+"'");
				}
				
				if(dCoulmns.size()>0) {
					String subSql = "insert into "+subTable+"("+dCoulmns.stream().collect(Collectors.joining(","))+")values("+dValues.stream().collect(Collectors.joining(","))+")";
					targetSql.add(subSql);
				}
			}

		}
	}
	
	public String getActiveDataSourcekey() {
		JSONObject target = JSON.parseObject(targetJSON);
		JSONObject tableConfig = JSON.parseObject(targetApi.getTableConfig());
		String configWhseid = Optional.ofNullable(tableConfig.getString("whseid")).orElse("").toLowerCase();
		String targetWhseid = Optional.ofNullable(target.getString("whseid")).orElse("").toLowerCase();
		String sqlMappingWhseid = "";
		if(globalMap != null) {
			Map<String, Object> map = Optional.ofNullable(globalMap.get("whseid")).orElse(new HashMap<String, Object>());
			sqlMappingWhseid = Optional.ofNullable((String) map.get("whseid")).orElse("").toLowerCase();
		}
		String whseid = "";
		
		if(StringUtils.hasLength(sqlMappingWhseid)&&(sqlMappingWhseid.startsWith("wmwhse")||sqlMappingWhseid.startsWith("enterprise"))) {
			whseid =sqlMappingWhseid;
		}else {
			if(StringUtils.hasLength(targetWhseid)) {
				if(targetWhseid.startsWith("wmwhse")||targetWhseid.startsWith("enterprise")) {
					whseid =targetWhseid;
				}else {
					if(configWhseid.startsWith("wmwhse")||configWhseid.startsWith("enterprise")) {
						whseid = configWhseid;
					}else {
						whseid = "wmwhse1";
					}
				}
			}else {
				if(configWhseid.startsWith("wmwhse")||configWhseid.startsWith("enterprise")) {
					whseid = configWhseid;
				}else {
					whseid = "wmwhse1";
				}
			}
		}
		
		return whseid;
		
	}


	public List<String> getTargetSql() {
		return targetSql;
	}


	public void setTargetSql(List<String> targetSql) {
		this.targetSql = targetSql;
	}
}
